Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring in scroll damper #49

Merged
merged 12 commits into from
Apr 7, 2024
Merged

Bring in scroll damper #49

merged 12 commits into from
Apr 7, 2024

Conversation

HaroldLever
Copy link
Contributor

@HaroldLever HaroldLever commented Apr 5, 2024

Bring.in.VelocityHelper.mp4

VelocityHelper has been renamed as ScrollDamper

Introduce ScrollDamper

I wrote a utility class that helps developers to handle velocity in convenience. ScrollDamper is an abstract class which shouldn't be used directly but to extend. For now, I brought in four different scroll dampers, they are LinearScrollDamper, QuadScrollDamper, CubicScrollDamper and ExpoScrollDamper. The last one is similar to the original way, and it's the default ScrollDamper once SmoothScrollContainer is created in editor.

image

How to use

slide()

Fill in parameters it needs, it will return the next velocity and relative offset (not position).

attract()

Emulate force that attracts something to destination. Return the result of next velocity according to delta time.

rebound_strength

The higher the value, the faster attract() attracts.

_calculate_velocity_to_dest()

Return the exact velocity that makes something slide to destination.

How to extend

To show how scroll damper works, here we take LinearScrollDamper as example.
In convenience of calculation, we assume that x axis is time, but time comes from infinity to zero.

_calculate_velocity_by_time(time)

For linear velocity, we let velocity slows down as time count down to zero, which is:

if time <= 0.0: return 0.0
return time * _factor

_factor is a private variable of LinearScrollDamper, using friction to set its value indirectly is a simpler way. The greater the value, the faster the velocity decrease.
The differential of this formula must greater than 0 no matter what "time" it is, or it might cause some issue.
image

_calculate_time_by_velocity(velocity)

It's the reverse calculation of the previous formula.

return abs(velocity) / _factor

Notice that no matter what sign velocity is, we regard it as positive.

_calculate_offset_by_time(time)

This is the integral for the formula in _calculate_velocity_by_time(time). Constant is not needed.

time = max(time, 0.0)
return 1.0/2.0 * _factor* time*time

_calculate_time_by_offset(offset)

It's the reverse calculation of the previous formula.

return sqrt(abs(offset) * 2.0 / _factor)

Notice

Need more testing: A big part of the script has been rewritten, since it is involved deeply.
Incompatible changes: Friction and damping is removed which is not a compatible change.

Other changes

Useful methods

get_spare_size()

Scroll bar takes up some space of the container, which might cause some calculation mistakes. Using get_spare_size() to get spare space size.
image

get_child_size_diff()

Get the size differences between child node and container. Child node 's scale and scroll bar size are taken into consideration. It could be useful for future development like content scaling.

get_child_boundary_dist()

Get the left, right, top and bottom distance.

Redesigned interface

Since the exposed properties are getting more, it is a good time to reorder these properties.
image

[Edited]

Tween for scroll_to()

Tween for scroll_to function will fight against scrolling and dragging input when tween animation is not done. Now all the scroll_to tween will be killed if needed.

@SpyrexDE
Copy link
Owner

SpyrexDE commented Apr 5, 2024

Thank you very much for your work on this pull request and its documentation! These are quite nice changes that will enhance the addon a lot.

There are some things I would suggest or would like to hear your opinion on:

  • Rename attracting strength to something more intuitive like damping, and update its docstring
  • Change the name “Helper” to something more descriptive for casual users
  • Scale down the span of values to be more userfriendly (e.g. max friction of 100 instead of 100000)
  • Instead of checking for editor hint in _init() use x_velocity_helper: VelocityHelper = xVelocityHelper.new()

Generally, I'd also like to conform with the official GDScript styling guidelines for Godot 4.

@HaroldLever
Copy link
Contributor Author

HaroldLever commented Apr 6, 2024

  • Rename attracting strength to something more intuitive like damping, and update its docstring
  • Change the name “Helper” to something more descriptive for casual users

I 'm really not good at naming, but I've tried some new changes.
VelocityHelperVelocityUtility
attracting_strengthmagnetism

  • Scale down the span of values to be more userfriendly (e.g. max friction of 100 instead of 100000)

I do have thought about this before. However, they are true parameters that formulas use. I 'm worrying about modifying factors might cause maintenance difficulties in the future.

@HaroldLever HaroldLever changed the title Bring in VelocityHelper Bring in velocity utility Apr 6, 2024
@SpyrexDE
Copy link
Owner

SpyrexDE commented Apr 6, 2024

What do you think of the names MomentumController and rebound_strength?

@HaroldLever
Copy link
Contributor Author

What do you think of the names MomentumController and rebound_strength?

Actually, before I brought these classes to this project, they were generic utility classes that helped me simplify formula calculations, which led me to be conservative in naming. Then I realized that since this is now utilities only for this project, I thought we could make the naming a little more specific.
rebound_strength sounds accurate and easy to understand, I 'm not sure whether Momentum would sound like there is a concept of mass. Prehaps ScrollDamper might be a good choice.
You can make commit with suitable name changes you like.

@SpyrexDE
Copy link
Owner

SpyrexDE commented Apr 6, 2024

Yeah, ScrollDamper is a good name. Do you have anything you want to add or can you spot something I missed out on? Otherwise, I would merge this and create a new 1.3 release.

@HaroldLever
Copy link
Contributor Author

HaroldLever commented Apr 7, 2024

  • Scale down the span of values to be more userfriendly (e.g. max friction of 100 instead of 100000)

I 've made some changes to narrow the range of parameters of scroll damper in the latest commit. See if it is you want.

Do you have anything you want to add or can you spot something I missed out on?

I guess it's all well and done.

@HaroldLever HaroldLever changed the title Bring in velocity utility Bring in scroll damper Apr 7, 2024
@SpyrexDE
Copy link
Owner

SpyrexDE commented Apr 7, 2024

I just noticed some stuttering on really slow movement, but that existed before already so I will create a new issue for that.
Edit: Here is the issue: #50

What is SmoothScrollContainer2 for, can it be removed?

@HaroldLever
Copy link
Contributor Author

What is SmoothScrollContainer2 for, can it be removed?

Ahh, my apologies. I added some casual testing changes in that commit.

@SpyrexDE SpyrexDE merged commit 167ef98 into SpyrexDE:godot-4 Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants